﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.IO.IsolatedStorage;
using IPMsgExpections;

namespace CommonUtility
{
    public class Log
    {
        //The object for Lock
        private static readonly object SyncRoot = new object();

        /// <summary>
        /// 记录运行信息日志
        /// </summary>
        /// <param name="message">信息</param>
        public static void Info(string message)
        {
            message = message.Replace('\0', (char)32);
            string currentTime = DateTime.Now.ToString();
            string logString = string.Format("{0}-INFO:{1}", currentTime, message);

            //写入控制台
            System.Diagnostics.Debug.WriteLine(logString);

            //写入日志文件
            try
            {
                WriteToFile(logString);
            }
            catch (Exception ex)
            {
                throw new FileExpection(ex.Message, ex);
            }
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message">信息</param>
        public static void Error(string message)
        {
            message = message.Replace('\0', (char)32);
            string currentTime = DateTime.Now.ToString();
            string logString = string.Format("{0}-ERROR:{1}", currentTime, message);

            //写入控制台
            System.Diagnostics.Debug.WriteLine(logString);

            //写入日志文件
            try
            {
                WriteToFile(logString);
            }
            catch (Exception ex)
            {
                throw new FileExpection(ex.Message, ex);
            }
        }

        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="message">信息</param>
        public static void Debug(string message)
        {
#if DEBUG
            message = message.Replace('\0', (char)32);
            string currentTime = DateTime.Now.ToString();
            string logString = string.Format("{0}-DEBUG:{1}", currentTime, message);

            //写入控制台
            System.Diagnostics.Debug.WriteLine(logString);

            //写入日志文件
            try
            {
                WriteToFile(logString);
            }
            catch (Exception ex)
            {
                throw new FileExpection(ex.Message, ex);
            }
#endif
        }

        /// <summary>
        /// 记录警告日志
        /// </summary>
        /// <param name="message">信息</param>
        public static void Warning(string message)
        {
            message = message.Replace('\0', (char)32);
            string currentTime = DateTime.Now.ToString();
            string logString = string.Format("{0}-WARNING:{1}", currentTime, message);

            //写入控制台
            System.Diagnostics.Debug.WriteLine(logString);

            //写入日志文件
            try
            {
                WriteToFile(logString);
            }
            catch (Exception ex)
            {
                throw new FileExpection(ex.Message, ex);
            }
        }

        /// <summary>
        /// 将文本写入日志文件（因为会有多个线程同时访问，所以需要加锁）
        /// </summary>
        /// <param name="txtToLog">要写入的文本</param>
        private static void WriteToFile(string txtToLog)
        {
            lock (SyncRoot)
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream isoStream = myIsolatedStorage.OpenFile("log.txt", System.IO.FileMode.Append))
                    {
                        using (StreamWriter writer = new StreamWriter(isoStream))
                        {
                            writer.WriteLine(txtToLog);
                        }
                    }
                }
            }
        }
    }
}
